<template>
  <!-- 表单模版 -->
  <el-dialog
    title="添加合同"
    :visible.sync="showRent"
    width="580px"
    @close="closeDialog"
  >
    <div class="form-container">
      <el-form
        ref="addForm"
        :model="rentForm"
        :rules="rentRules"
        label-position="top"
      >
        <el-form-item label="租赁楼宇" prop="buildingId">
          <el-select
            v-model="rentForm.buildingId"
            placeholder="请选择"
            @visible-change="getRentBuildList"
          >
            <el-option
              v-for="item in RentBuildList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="租赁起止日期" prop="rentTime">
          <el-date-picker
            v-model="rentForm.rentTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="租赁合同" prop="contractId">
          <input
            ref="UploadRef"
            type="file"
            class="upload-input"
            @change="uploadChange"
          >
          <el-button type="text" @click="upload">点击上传</el-button>
          <p>{{ rentForm.name }}</p>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button size="mini" @click="closeDialog">取 消</el-button>
      <el-button
        size="mini"
        type="primary"
        @click="submitRent"
      >确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {
  getRentBuildListAPI,
  uploadAPI,
  createRentAPI
} from '@/api/enterprise'
export default {
  props: {
    showRent: {
      type: Boolean,
      default: false
    },
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      RentBuildList: [],
      rentForm: {
        buildingId: null, // 楼宇id
        contractId: null, // 合同id
        contractUrl: '', // 合同Url
        enterpriseId: null, // 企业名称
        type: 0, // 合同类型
        rentTime: [] // 合同时间
      },
      rentRules: {
        buildingId: [
          { required: true, message: '请选择楼宇', trigger: 'change' }
        ],
        rentTime: [
          { required: true, message: '请选择租赁日期', trigger: 'change' }
        ],
        contractId: [{ required: true, message: '请上传合同文件' }]
      }
    }
  },
  created() {
    console.log(this.id)
  },
  methods: {
    async submitRent() {
      const res = await this.$refs.addForm.validate()
      if (res) {
        const { buildingId, contractId, contractUrl, type } =
          this.rentForm
        console.log(this.rentForm.rentTime)
        await createRentAPI({
          buildingId,
          contractId,
          contractUrl,
          enterpriseId: this.id,
          type,
          startTime: this.rentForm.rentTime[0],
          endTime: this.rentForm.rentTime[1]
        })

        // 关闭弹框
        this.$emit('update:showRent', false)
        // 重置表单
        this.$refs.addForm.resetFields()
        this.rentForm.contractUrl = ''
        this.rentForm.contractId = ''
      }
    },

    closeDialog() {
      this.$emit('update:showRent', false)
      this.$refs.addForm.resetFields()
    },
    upload() {
      this.$refs.UploadRef.click()
    },
    async uploadChange(e) {
      const files = e.target.files
      if (files.length > 0) {
        const file = files[0]
        const fd = new FormData()
        fd.append('type', 'contract')
        fd.append('file', file)
        try {
          const res = await uploadAPI(fd)
          console.log(res)
          this.rentForm.contractId = res.data.id
          this.rentForm.contractUrl = res.data.url
          console.log(this.rentForm)
          this.$message.success('上传成功')
        } catch (error) {
          this.$message.error('上传失败')
        } finally {
          this.$refs.UploadRef.value = ''
        }
      }
    },
    async getRentBuildList() {
      if (!this.RentBuildList) {
        return
      } else {
        const res = await getRentBuildListAPI()
        this.RentBuildList = res.data
        // console.log(res)
        // console.log(this.RentBuildList)
      }
    }
  }
}
</script>
    <style>
.upload-input {
  display: none;
  z-index: -9999;
}
</style>

